有哪位高手指点一下st(state threads)的上下文切换问题吗

bravery36 2015-03-12 08:27:21
问题很简单,我用st线程执行st_write的时候,上下文不会切换。我试过用st_usleep(0)是能切换的,在文档说明中st_write是能切换上下文的,不然这东西就没用了,求高手指点用法。附简单源码。

static int g_count = 0;
void* do_calc(void* arg)
{
int iIndex = *(int *)arg;
int iRet = 0;
static int iMBCount = 0;

delete (int *)arg;

SrsSTFileWriter fs;
std::string file = "/mnt/s3/vod/test/test";
char cTmp[32] = {0};
st_netfd_t fd = NULL;
ssize_t nwrite = 0;

sprintf(cTmp, "%u.dat", iIndex);
file += cTmp;

do
{

int flags = O_CREAT | O_WRONLY | O_TRUNC;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;

if ((fd = st_open(file.c_str(), flags, mode)) == NULL)
{
iRet = ERROR_SYSTEM_FILE_OPENE;
printf("open file %s failed. ret=%d\n", file.c_str(), iRet);
break;
}
if ((nwrite = st_write(fd, "aaa", 3, ST_UTIME_NO_TIMEOUT)) < 0)
{
iRet = ERROR_SYSTEM_FILE_WRITE;
printf("write to file %s failed. ret=%d\n", file.c_str(), iRet );
break;
}
//st_usleep(0);

if (st_netfd_close(fd) < 0)
{
iRet = ERROR_SYSTEM_FILE_CLOSE;
printf("close file %s failed. ret=%d\n", file.c_str(), iRet);
break;
}
}while (1);


return NULL;
}

int run()
{
if(st_init() < 0)
{
printf("error!\n");
return -1;
}

for(int i = 1; i <= g_count; i++)
{
int *piArg = new int(i);
if(st_thread_create(do_calc, (void*)piArg, 0, 0) == NULL)
{
printf("error!\n");
return -1;
}
else
{
printf("thread %d create!\n" , i);
}
}

st_thread_exit(NULL);

return 0;
}

int main(int argc, char **argv)
{
int ret = ERROR_SUCCESS;


if (argc <= 1)
{
printf("Test the concurrence of state-threads!\n"
"Usage: %s <sthread_count>\n"
"eg. %s 10000\n", argv[0], argv[0]);

return ret;
}
g_count = atoi(argv[1]);

return run();
}
...全文
270 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-03-13
  • 打赏
  • 举报
回复
如果st_write到打印机,估计就会切换上下文。 搜“线程调度 毛德操”
bravery36 2015-03-13
  • 打赏
  • 举报
回复
自己顶起来一下。有大侠愿意帮下忙吗
赵4老师 2015-03-13
  • 打赏
  • 举报
回复
引用 4 楼 bravery36 的回复:
自己跟了一下代码,发现是每次write的时候都写成功了整个buf,没有切上下文的机会,也许是linux的write的缓冲比较大。就赵老师回贴,就给赵老师好了。
笑纳了。
bravery36 2015-03-13
  • 打赏
  • 举报
回复
自己跟了一下代码,发现是每次write的时候都写成功了整个buf,没有切上下文的机会,也许是linux的write的缓冲比较大。就赵老师回贴,就给赵老师好了。
bravery36 2015-03-12
  • 打赏
  • 举报
回复
代码是我自己去掉一些多余内容后贴上来的,应该是编不了,请不要在意细节

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧