softkasce.blogg.se

Passing value in to pthread c
Passing value in to pthread c




passing value in to pthread c

PASSING VALUE IN TO PTHREAD C HOW TO

Exampleįigure 11.3 shows how to fetch the exit code from a thread that has terminated. In this case, calling pthread_join allows us to wait for the specified thread, but does not retrieve the thread’s termination status. If we’re not interested in a thread’s return value, we can set rval_ptr to NULL. If the thread was already in the detached state, pthread_join can fail, returning EINVAL, although this behavior is implementation-specific. If the thread was canceled, the memory location specified by rval_ptr is set to PTHREAD_CANCELED.īy calling pthread_join, we automatically place the thread with which we’re joining in the detached state (discussed shortly) so that its resources can be recovered. If the thread simply returned from its start routine, rval_ptr will contain the return code. The calling thread will block until the specified thread calls pthread_exit, returns from its start routine, or is canceled. Returns: 0 if OK, error number on failure.So we pass the value 0 to its core_id.Int pthread_join(pthread_t thread, void ** rval_ptr) For example, we want the first pthread to camp on the first CPU core which has the ID 0. Create the threads and initialize the core_id argument, which will be used to set the thread to the specific CPU core. Struct thread_info *thread_info = calloc(num_threads, sizeof(struct thread_info)) Allocate memory for pthread_create() arguments Print_error_then_terminate(setstacksize_result, "pthread_attr_setstacksize") We will set the stack size limit to is 1 MB (0x100000 bytes)Ĭonst int setstacksize_result = pthread_attr_setstacksize(&attr, stack_size) Print_error_then_terminate(attr_init_result, "pthread_attr_init") Snprintf(buffer, needed, FAILURE_MSG, pid, core_id) Ĭonst int attr_init_result = pthread_attr_init(&attr) Snprintf(buffer, needed, SUCCESS_MSG, pid, core_id) Ĭonst size_t needed = snprintf(NULL, 0, FAILURE_MSG, pid, core_id) CPU_ISSET: This macro returns a nonzero value (true) if cpu is a member of the CPU set set, and zero (false) otherwise.Ĭonst size_t needed = snprintf(NULL, 0, SUCCESS_MSG, pid, core_id) Print_error_then_terminate(get_affinity, "pthread_getaffinity_np") pthread_getaffinity_np: The pthread_getaffinity_np() function returns the CPU affinity mask of the thread thread in the buffer pointed to by cpuset.Ĭonst int get_affinity = pthread_getaffinity_np(pid, sizeof(cpu_set_t), &cpuset) Check what is the actual affinity mask that was assigned to the thread. Print_error_then_terminate(set_result, "pthread_setaffinity_np") If the call is successful, and the thread is not currently running on one of the CPUs in cpuset, then it is migrated to one of those CPUs.Ĭonst int set_result = pthread_setaffinity_np(pid, sizeof(cpu_set_t), &cpuset) pthread_setaffinity_np: The pthread_setaffinity_np() function sets the CPU affinity mask of the thread thread to the CPU set pointed to by cpuset. CPU_SET: This macro adds cpu to the CPU set set. CPU_ZERO: This macro initializes the CPU set set to be the empty set.

passing value in to pthread c passing value in to pthread c

cpu_set_t: This data set is a bitset where each bit represents a CPU. #define FAILURE_MSG "Failed to set thread %lu to affinity to CPU %d\n"Ĭonst int core_id = thread_info->core_id #define SUCCESS_MSG "Successfully set thread %lu to affinity to CPU %d\n" Int core_id // Core ID we want this pthread to set its affinity to Pthread_t thread_id // ID returned by pthread_create() #define print_error_then_terminate(en, msg) \ĭo while (0) The header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong. Printf("Set returned by pthread_getaffinity_np() contained:\n") Handle_error_en(s, "pthread_getaffinity_np") S = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset) * Check the actual affinity mask assigned to the thread */ Handle_error_en(s, "pthread_setaffinity_np") S = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset) * Set affinity mask to include CPUs 0 to 7 */ The selection is made with the variable speid (that is user defined) and contains a number from 0 to (CPU NUMBER – 1). The following code sets the affinity of each pthread to a different and specific CPU core. 24 February 2012 in C / C++ / Programming tagged affinity / C++ / CPU / pthread by Tux






Passing value in to pthread c