pthreads

Memory leaks when using pthead_exit() to exit thread

耗尽温柔 提交于 2020-01-03 04:14:09
问题 I have a problem when use pthread_exit() to exit .My code is like this: { ... pthread_attr_t attr; iRetValue = pthread_attr_init(&attr); iRetValue = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); size_t nStackSize = 4 * 1024 * 1024; iRetValue = pthread_attr_setstacksize(&attr, nStackSize); while(condition) { ... int32 iSockId = cServSocket.accept(); if(iSockId < 0) { continue; } pthread_t tid; int32* pSockId = new int(iSockId); iRetValue = pthread_create(&tid, &attr, run_thread,

read same structure on all threads

末鹿安然 提交于 2020-01-03 03:40:12
问题 I want all threads to read from the same structure. I did it in the past by adding threads inside the loop which reads from the structure but this time I need the structure to be opened inside void "dowork" as my example shows. I have the following code: struct word_list { char word[20]; struct word_list * next; }; struct word_list * first_word = NULL; //other function which loads into struct is missing cause it's not relevant //in main() pthread_t thread_id[MAX_THREADS]; int max_thread = 10;

Multiple threads can wait on a semaphore at same time

风格不统一 提交于 2020-01-03 03:32:10
问题 Can multiple threads wait on a single semaphore ? If yes, upon semaphore down which one will be resumed ? 回答1: As the document for sem_post suggests, If the value of the semaphore resulting from this operation is zero, then one of the threads blocked waiting for the semaphore will be allowed to return successfully from its call to sem_wait() . If the symbol _POSIX_PRIORITY_SCHEDULING is defined, the thread to be unblocked will be chosen in a manner appropriate to the scheduling policies and

Allocating a Thread's Stack on a specific NUMA memory

狂风中的少年 提交于 2020-01-02 21:51:53
问题 I would like to know if there is a way to create the stack of a thread on a specific NUMA node. I have written this code but i'm not sure if it does the trick or not. pthread_t thread1; int main(int argc, char**argv) { pthread_attr_t attr; pthread_attr_init(&attr); char** stackarray; int numanode = 1; stackarray = (char**) numa_alloc_onnode(sizeof(char*), numanode); // considering that the newly // created thread will be running on a core on node1 pthread_attr_setstack(&attr, stackarray[0],

What is the official standard for pthreads?

穿精又带淫゛_ 提交于 2020-01-02 20:22:19
问题 I am trying to find the document that specifies the standard for pthreads. I've seen various links which point to IEEE 1003.1c-1995 (i.e. Wikipedia or OpenGroup). However when I searched for this document on the IEEE standards site I eventually found this page which said "Superseded Standard." The IEEE page for 1003.1c-1995 did have a note that said: "Abstract not available. See ISO/IEC 9945-1." Searching for that on Google led me to a page for ISO/IEC 9945-1:1996 but the status said

MySQLi wont establish connection within a pthreads worker

别等时光非礼了梦想. 提交于 2020-01-02 15:01:23
问题 Why cannot a mysqli connection be created when using a pthread worker in PHP? When establishing a mysqli and attempting to execute a query, will generatate an error PHP Warning: mysqli::query(): Couldn't fetch mysqli in W:\workspace\Sandbox\application.php on line 47 This is my script <?php // Application // Exceptions class MySQLWorkerException extends Exception {} class MySQLConnectionException extends Exception {} class MySQLQueryException extends Exception {} // Worker class SQLWorker

Efficient and fast way for thread argument

。_饼干妹妹 提交于 2020-01-02 07:13:13
问题 What is the most efficient way to create a thread with argument? The argument is a struct, if the struct can not stay on parent thread stack, there are two solutions. With dynamic memory allocation struct Arg{ int x; int y; }; void* my_thread(void* v_arg){ Arg* arg = (Arg*) v_arg; //... running delete arg; return NULL; } //Creating a thread void a_function(){ Arg* arg = new Arg; arg->x = 1; arg->y = 2; pthread_t t; pthread_create(&t, NULL, my_thread, arg); pthread_detach(t); } With semaphore

Matrix multiplication using multiple threads?

南笙酒味 提交于 2020-01-02 06:07:36
问题 I am supposed to multiply 2 matrices using threads. Two things: I keep getting 0's when I run the program. I also get message errors(for each, it says "warning: passing argument 1 of 'printMatrix' from incompatible pointer type" on the bolded lines(where I try to print the output). Also to note, the first block that is bolded, I that was my attempt at solving the problem. I think I am close, but I may not be. Can anyone help? Thanks :) Output looks like this: A= 1 4 2 5 3 6 B= 8 7 6 5 4 3 A*B

-pthread, -lpthread and minimal dynamic linktime dependencies

删除回忆录丶 提交于 2020-01-02 05:46:08
问题 This answer suggest -pthread is preferable to -lpthread because predefined macros. Empirically, -pthread gives me only one extra macro: #define _REENTRANT 1 and it also appears to force libpthread.so.0 as a dynamic linktime dependency. When I compile with -lpthread , that dependency is only added if I actually call any of the pthread functions. This is preferably to me, because then I wouldn't have to treat multithreaded programs differently in my build scripts. So my question is, what else

c - global variables in pthreads

有些话、适合烂在心里 提交于 2020-01-02 05:19:27
问题 Suppose I defined a function in file function.c, and in main.c I create multiple pthreads to execute the function in function.c. If in function.c, I define a global variable, for example, int foo; Then, my question is, does every thread has its own instance of this variable "foo" or do they share a single "foo"? 回答1: They share a single foo variable. Global variable always exists only once per process and is usually protected by mutex to avoid concurrent access. Since C11 you can use thread