有哪位高手指点一下st(state threads)的上下文切换问题吗
问题很简单,我用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();
}