Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C linux pthread需要睡眠才能工作_C_Linux_Gcc_Pthreads - Fatal编程技术网

C linux pthread需要睡眠才能工作

C linux pthread需要睡眠才能工作,c,linux,gcc,pthreads,C,Linux,Gcc,Pthreads,我正在使用gcc在linux上用c编写一个程序。 如果我没有使用睡眠声明 它将随机打印“线程创建”2、3或4次。有人能解释我的这种行为吗? //下面的代码只是一个示例,我知道创建一个线程只是为了打印一个字符串并不是很有用:) void*makeRequest(void*arg){ printf(“线程创建\n”); } int main(){ pthread_t thr[10]; 对于(i=0;i您应该加入您创建的所有线程,结束main,就像退出整个过程一样 或者,您可以使用pthread\u

我正在使用gcc在linux上用c编写一个程序。 如果我没有使用睡眠声明 它将随机打印“线程创建”2、3或4次。有人能解释我的这种行为吗? //下面的代码只是一个示例,我知道创建一个线程只是为了打印一个字符串并不是很有用:)

void*makeRequest(void*arg){
printf(“线程创建\n”);
}
int main(){
pthread_t thr[10];

对于(i=0;i您应该加入您创建的所有线程,结束
main
,就像退出整个过程一样

或者,您可以使用
pthread\u exit
结束
main
,您需要:

for(i = 0; i < 3; i++)
{
   pthread_join(thr[i], NULL);
}
(i=0;i<3;i++)的

{
pthread_join(thr[i],NULL);
}

正如您所写的,在main返回并退出整个进程之前,没有任何东西等待线程完成,从而终止线程。

首先使用join:,如果不使用它,则无需发送
&i

for(i=0; i<3; i++){  
       pthread_create(&thr[i], 0, makeRequest, &i);   
}

for(i=0; i<3; i++){  
       pthread_join(thr[i], NULL);
}
随机打印“线程创建”2、3或4次

当主线程死亡时,所有子线程也会死亡。所以您需要等待加入

随机。:因为我们不知道线程内上下文切换哪个会有机会。它可能是主线程或子线程

for(i=0; i<3; i++){  
       pthread_create(&thr[i], 0, makeRequest, &i);   
}

for(i=0; i<3; i++){  
       pthread_join(thr[i], NULL);
}
已编辑

-pthread
使用“pthreads”库添加对多线程的支持。此选项为预处理器和链接器设置标志:


除其他备注外

 pthread_create(&thr[i], 0, makeRequest, &i);
是不正确的,因为
i
是一个局部变量,所以
&i
在所有调用上都是相同的指针

通常,您应该将数据指针设置为线程例程-这里的线程例程是
makeRequest
静态指针或唯一指针(每个线程都是唯一的);实际上,将其设置为指向某个
malloc
-ed备忘录的指针

更好的做法是声明一些
struct my\u thread\u data\u st
,以便在堆中使用

struct my_thread_data_st* td = malloc(sizeof(struct my_thread_data_st));
if (!td) perror("malloc td"), exit(EXIT_FAILURE);
memset (td, 0, sizeof(struct my_thread_data_st));
// fill td appropriately, then
pthread_create(&thr[i], 0, makeRequest, td);
或者您可以拥有一个数组,例如
int
-s,例如
intnum[4];
,对其进行适当的初始化,然后
pthread\u创建(&thr[i],0,makeRequest,&num[i]);

当然,如果
td
是通过
malloc
进行堆分配的,不要忘记在适当的时间
释放它,例如在线程结束后(例如,在对它进行了-ed之后)。您可能还对
GC\u malloc
感兴趣并使用它,而不是
malloc
(那么,不要担心释放内存,GC会这样做)

如果线程正在访问共享数据,则应使用一些[全局或静态]互斥锁(使用
pthread\u mutex\u lock
&
pthread\u mutex\u unlock
)序列化对共享数据的访问

在退出之前,不要忘记在所有线程上调用
pthread\u join
——例如从
main
返回


我建议读一些关于的书。

所以pthread连接必须在for语句之后进行(我的程序非常复杂,而且“for”语句必须无限次执行,因为它等待客户端请求;当客户端请求存在时,它将创建3个线程)
while(1){for(i=0;i您只需确保您的
main
不会在任何其他线程之前结束。因此,是的,连接必须在
for
循环中创建线程和
main
终止之间的某个位置。感谢-lpthread建议,我将阅读更多关于此的内容//我知道我不需要发送那个“i”,但正如我之前所说的,这只是一个示例,(整个代码大约有500行)@nicknatar但我不认为
“thread created”
打印4次,它可能最多打印3次。@GrijeshChauhan,
-pthread
是gcc的正确选项。它比
-lpthread
做的更多:“
-pthread
使用“pthreads”库添加对多线程的支持。此选项为预处理器和链接器设置标志。”@JensGustedt:Oh..我不知道-pthread:(.我当时确实弄错了…但我在google-search中没有找到此选项。非常感谢您非常详细的回答。这对我的堆内存分配有很大帮助。建议。再次非常感谢:)
struct my_thread_data_st* td = malloc(sizeof(struct my_thread_data_st));
if (!td) perror("malloc td"), exit(EXIT_FAILURE);
memset (td, 0, sizeof(struct my_thread_data_st));
// fill td appropriately, then
pthread_create(&thr[i], 0, makeRequest, td);