多线程的问题
#include <stdio.h>
#include <malloc.h>
#include <stddef.h>
#include <stdlib.h>
#include <pthread.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define N 5
pthread_t ntid;
int nChildCount=0;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
nChildCount--;
}
void *thr_fn(void *arg)
{
nChildCount++;
printids(arg); return NULL;
}
int main(int argc, char* argv[])
{
int i;
for(i=0;i<8;i++)
{
int err;
char strI[12];
//sprintf(strI,"the i is :%d,child thread ",i);如果这行程序加上去的话,只能创建一个线程,如果注释掉话,就可以创建八个线程.
err = pthread_create(&ntid, NULL, thr_fn, "");
pthread_join(ntid, NULL);
if (err != 0)
{
fprintf(stderr, "can't create thread: %s\n", strerror(err)); exit(1);
}
}
return 0;
}
int getx(int x,int y)
{
return x+y;
}
上面有一行程序
sprintf(strI,"the i is :%d,child thread ",i);如果这行程序加上去的话,只能创建一个线程,如果注释掉话,就可以创建八个线程.求高手解释一下
不注释掉的话情况,运行的结果是
pid 15654 tid 2 (0x2)
注释掉这行程序后运行的结果是
pid 19631 tid 2 (0x2)
pid 19631 tid 3 (0x3)
pid 19631 tid 4 (0x4)
pid 19631 tid 5 (0x5)
pid 19631 tid 6 (0x6)
pid 19631 tid 7 (0x7)
pid 19631 tid 8 (0x8)
pid 19631 tid 9 (0x9)
#include <malloc.h>
#include <stddef.h>
#include <stdlib.h>
#include <pthread.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define N 5
pthread_t ntid;
int nChildCount=0;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
nChildCount--;
}
void *thr_fn(void *arg)
{
nChildCount++;
printids(arg); return NULL;
}
int main(int argc, char* argv[])
{
int i;
for(i=0;i<8;i++)
{
int err;
char strI[12];
//sprintf(strI,"the i is :%d,child thread ",i);如果这行程序加上去的话,只能创建一个线程,如果注释掉话,就可以创建八个线程.
err = pthread_create(&ntid, NULL, thr_fn, "");
pthread_join(ntid, NULL);
if (err != 0)
{
fprintf(stderr, "can't create thread: %s\n", strerror(err)); exit(1);
}
}
return 0;
}
int getx(int x,int y)
{
return x+y;
}
上面有一行程序
sprintf(strI,"the i is :%d,child thread ",i);如果这行程序加上去的话,只能创建一个线程,如果注释掉话,就可以创建八个线程.求高手解释一下
不注释掉的话情况,运行的结果是
pid 15654 tid 2 (0x2)
注释掉这行程序后运行的结果是
pid 19631 tid 2 (0x2)
pid 19631 tid 3 (0x3)
pid 19631 tid 4 (0x4)
pid 19631 tid 5 (0x5)
pid 19631 tid 6 (0x6)
pid 19631 tid 7 (0x7)
pid 19631 tid 8 (0x8)
pid 19631 tid 9 (0x9)
...全文
相关推荐
DateFormat多线程问题
在多线程环境下,不恰当的使用`DateFormat`可能导致数据不一致、性能下降甚至程序崩溃。这篇博客将深入探讨`DateFormat`在多线程环境下的问题及其解决方案。
浅析Python多线程下的变量问题
然而,当需要在线程间共享数据时,就必须处理好线程同步问题。3. 数据传递问题:在多线程编程中,将数据从一个线程传递到另一个线程也是一个挑战。
多线程死锁问题
在Java多线程编程中,死锁是一个非常重要的概念,也是开发者需要密切关注和避免的问题。死锁是指两个或多个线程互相等待对方释放资源,从而导致它们都无法继续执行的状态。
Python tkinter 多线程 哲学家进餐问题
"Python tkinter 多线程实现的哲学家进餐问题示例代码"在计算机科学中,"哲学家进餐问题"是一个经典的并发控制问题,用来展示死锁的可能性。这个问题描述了五个哲学家围坐在一张圆桌旁,每
个人总结40个Java多线程面试问题和答案
Java多线程的缺点是增加了程序的复杂度,可能会导致deadlock和数据不一致的问题。13. 如何使用多线程?


