linux C 多线程获取字符输入方法
2016-05-18 13:49
519 查看
#define READ_BUF_LEN 8
pthread_t lTestThread;
static void *doTestThreadFunc(void *parg)
{
char acBuf[READ_BUF_LEN];
char cCounti;
int nTestData;
while(1)
{
memset(acBuf, 0, READ_BUF_LEN);
printf("input:");
fgets(acBuf, READ_BUF_LEN, stdin);
for(cCounti=0; cCounti<READ_BUF_LEN; cCounti++)
{
if(acBuf[cCounti] == '\n')
{
acBuf[cCounti] = '\0';
break;
}
}
printf("-- %s --\n", acBuf);
nTestData = strtol(acBuf, NULL, 0);
printf("0x%x \n", nTestData);
startSettingFunc(nTestData);
}
pthread_exit(NULL);
}
容易出错的地方:
1)必须明确使用 fgets 的长度,谨慎使用 strlen() 函数,当没有输入时,strlen() 获取到的长度就为 0,是错的;
2)线程互斥锁只能锁临界资源,不能锁再线程 while(1) 外面,容易导致死锁;
pthread_t lTestThread;
static void *doTestThreadFunc(void *parg)
{
char acBuf[READ_BUF_LEN];
char cCounti;
int nTestData;
while(1)
{
memset(acBuf, 0, READ_BUF_LEN);
printf("input:");
fgets(acBuf, READ_BUF_LEN, stdin);
for(cCounti=0; cCounti<READ_BUF_LEN; cCounti++)
{
if(acBuf[cCounti] == '\n')
{
acBuf[cCounti] = '\0';
break;
}
}
printf("-- %s --\n", acBuf);
nTestData = strtol(acBuf, NULL, 0);
printf("0x%x \n", nTestData);
startSettingFunc(nTestData);
}
pthread_exit(NULL);
}
容易出错的地方:
1)必须明确使用 fgets 的长度,谨慎使用 strlen() 函数,当没有输入时,strlen() 获取到的长度就为 0,是错的;
2)线程互斥锁只能锁临界资源,不能锁再线程 while(1) 外面,容易导致死锁;
相关文章推荐
- Linux socket 初步
- Linux Kernel 4.0 RC5 发布!
- Python3写爬虫(四)多线程实现数据爬取
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程